﻿package com.cf.design.effect{
	import flash.display.*;
	import flash.events.*;
	import com.cf.core.Utils.tools;
	public class FreeFall extends Sprite{
		private var __radius:Number;
		private var __angle:Number;
		private var __speed:Number;
		private var __top:Number;
		private var __left:Number;
		private var __right:Number;
		private var __bottom:Number;
		private var __img:*;
		public function get IMG():*{return __img}
		public function set IMG(_o:*):void{if(_o!=null){__img=_o;renew();}}
		
		public function FreeFall(){
			init();
			startFall();
			addEventListener(Event.ENTER_FRAME,handleFalling);
		}//fn
		public function area(_l:Number=0, _t:Number=0, _r:Number=300, _b:Number=300){
			__left=_l;
			__top=_t;
			__right=_r;
			__bottom=_b;
		}//fn
		private function init():void{
			// just in case, if not init
			area();
			__radius=Math.random()*2;
			IMG=new Sprite();
			IMG.graphics.clear();
			IMG.graphics.beginFill(0xEEEEFF);
			IMG.graphics.drawCircle(0,0,__radius);
			IMG.graphics.endFill();
			addChild(IMG);
		}//fn
		private function renew():void{
			tools.emptySprite(this);
			addChild(IMG);
		}//fn
		private function startFall():void{
			__angle=(Math.random()+0.5)*Math.PI/2;
			__speed=Math.random()+1;
			x=(Math.random()*(__right-__left))
			if(x < ((__right-__left)/2) ){
				x += (Math.random()*(__right-__left)/2);
			}//if
			y=__top;
			//alpha=Math.random();
		}//fn
		private function handleFalling(e:Event):void{
			x+=__speed*Math.cos(__angle);
			y+=__speed*Math.sin(__angle);
			if(y<__top||y>__bottom||x<__left||x>__right){
				// 復位重算
				startFall();
			}//if
		}//fn
	}//class
}//package